<template>
  <div>
    <el-breadcrumb separator="/">
      <el-breadcrumb-item :to="{ path: '/home' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>数据统计</el-breadcrumb-item>
      <el-breadcrumb-item>数据报表</el-breadcrumb-item>
    </el-breadcrumb>
    <!-- 卡片视图 -->
    <el-card>
      <!-- 2.Dom容器 -->
      <div ref="main" id="main" style="width: 700px; height: 450px"></div>
    </el-card>
  </div>
</template>

<script>
import * as echarts from 'echarts'
import _ from 'lodash'
import { reportApi } from '@/api/report'
export default {
  data() {
    return {
      // option选项数据(我们需要满足图表的基本参数)
      baseOption: {
        title: {
          text: '用户来源'
        },
        tooltip: {
          trigger: 'axis',
          axisPointer: {
            type: 'cross',
            label: {
              backgroundColor: '#E9EEF3'
            }
          }
        },
        grid: {
          left: '3%',
          right: '4%',
          bottom: '3%',
          containLabel: true
        },
        xAxis: [
          {
            boundaryGap: false
          }
        ],
        yAxis: [
          {
            type: 'value'
          }
        ]
      }
    }
  },
  async mounted() {
    // 1.基于准备好的dom，初始化echarts实例
    const myChart = echarts.init(document.querySelector('#main'))
    // 2.配置选项:由基本的图表结构+接口返回的图表数据组合才能正常使用
    const res = await reportApi()
    if (res.meta.status !== 200) return this.$message.error('获取数据失败')
    const option = _.merge(this.baseOption, res.data)
    // 3.使用刚指定的配置项和数据显示图表。
    myChart.setOption(option)
  }
}
</script>

<style lang="scss" scoped></style>